package com.zmn.oms.business.impl.conf.cancel;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.oms.business.interfaces.conf.cancel.OrderCancelCategoryBService;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.conf.cancel.OrderCancelCategory;
import com.zmn.oms.services.interfaces.conf.cancel.OrderCancelCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author tanbiao
 * @description
 * @since 2019/10/22 14:30
 * @modifier
 **/
@Service
public class OrderCancelCategoryBServiceImpl implements OrderCancelCategoryBService {
    @Autowired
    private OrderCancelCategoryService orderCancelCategoryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAll(OrderCancelCategory orderCancelCategory, String creater) throws OmsBaseException {
        // 检查名称是否重复
        OrderCancelCategory checkCategory = orderCancelCategoryService.findByName(orderCancelCategory.getName());
        if (checkCategory != null) {
            throw new OmsBaseException("新增分类失败，分类名称不能重复！");
        }
        orderCancelCategory.setCreater(creater);
        orderCancelCategory.setCreateTime(new Date());
        orderCancelCategory.setUpdater(creater);
        orderCancelCategory.setUpdateTime(new Date());
        orderCancelCategory.setStatus(GlobalDict.STATUS_ENABLE);
        orderCancelCategory.setFirstLetter(InitialUtil.getFristEnName(orderCancelCategory.getName()));
        orderCancelCategoryService.insert(orderCancelCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAll(OrderCancelCategory orderCancelCategory, String updater) throws OmsBaseException {
        // 检查名称是否重复
        OrderCancelCategory checkCategory = orderCancelCategoryService.findByName(orderCancelCategory.getName());
        if (checkCategory != null && !checkCategory.getCategId().equals(orderCancelCategory.getCategId())) {
            throw new OmsBaseException("分类名称不能重复！");
        }
        OrderCancelCategory cancelCategory = orderCancelCategoryService.findByKey(orderCancelCategory.getCategId());
        cancelCategory.setUpdater(updater);
        cancelCategory.setUpdateTime(new Date());
        cancelCategory.setName(orderCancelCategory.getName());
        cancelCategory.setSort(orderCancelCategory.getSort());
        cancelCategory.setFirstLetter(InitialUtil.getFristEnName(orderCancelCategory.getName()));
        orderCancelCategoryService.updateByKey(cancelCategory);
    }
}
